//
//  ICMSelecteStepInTimeCell.m
//  ICrEAM
//
//  Created by rainimute on 16/11/3.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ICMSelecteStepInTimeCell.h"

@interface ICMSelecteStepInTimeCell ()

@property (nonatomic, assign) float itemWidth;

@property (nonatomic, strong) NSMutableArray *selecteTimeButtons;

@property (nonatomic, strong) NSMutableArray *didSelectedTimes;

@end

@implementation ICMSelecteStepInTimeCell
#pragma mark    设置界面
- (void)setupSelecteStepInTimeCellWithDidSelectedTimes:(NSMutableArray *)didSelectedTimes andCurrentSelectedTimes:(NSArray *)currentSelectedTimes andOnlineSource:(NSMutableArray*)onlineSourceTimes andItemWidth:(float)itemWidth{
    self.itemWidth = itemWidth;
    self.didSelectedTimes = didSelectedTimes;
    for (NSInteger i=0; i<52; i++) {
        UIButton *currentButton = [self.selecteTimeButtons objectAtIndex:i];
        NSString *currentString = [@(i) stringValue];
        if ([didSelectedTimes containsObject:currentString]) {
            currentButton.backgroundColor = [self colorForState:1];
        }else if ([currentSelectedTimes containsObject:currentString]){
            currentButton.backgroundColor = [self colorForState:2];
        }else if ([onlineSourceTimes containsObject:currentString]){
            currentButton.backgroundColor = [self colorForState:3];
        }else{
            currentButton.backgroundColor = [self colorForState:0];
        }
    }
}

- (UIColor*)colorForState:(NSInteger)state{
    NSArray *colors = @[[UIColor whiteColor],TextFeildBackgroundColor_Gray,[ICrEAMTool isVW]?Color(0x1E80D1):Color(0x338D1B),[UIColor colorWithHexString:@"43CD80"]];
    return colors[state];
}
#pragma mark    选取时间格
- (void)selecteTimeButtonAction:(UIButton*)sender{
    NSInteger index = [self.selecteTimeButtons indexOfObject:sender];
    if ([self.didSelectedTimes containsObject:[@(index)stringValue]]) {
        return;
    }
    UITableView *tableView = (UITableView *)self.superview.superview;
    NSIndexPath *indexPath = [tableView indexPathForCell:self];
    
    if ([self.delegate respondsToSelector:@selector(tableView:didSelectRowAtIndexPath:andButtonIndex:)]) {
        [self.delegate tableView:tableView didSelectRowAtIndexPath:indexPath andButtonIndex:index];
    }
}

- (NSMutableArray *)selecteTimeButtons{
    if (!_selecteTimeButtons) {
        _selecteTimeButtons = [NSMutableArray array];
        for (NSInteger i=0; i<52; i++) {
            UIButton *selecteTimeButton = [[UIButton alloc]initWithFrame:CGRectMake(i*self.itemWidth, 0, self.itemWidth, self.itemWidth)];
            BorderWidth(selecteTimeButton, 1);
            selecteTimeButton.layer.borderColor = Color(BorderColor_Gray_Hex).CGColor;
            [selecteTimeButton addTarget:self action:@selector(selecteTimeButtonAction:) forControlEvents:UIControlEventTouchUpInside];
            [self.contentView addSubview:selecteTimeButton];
            [_selecteTimeButtons addObject:selecteTimeButton];
        }
    }
    return _selecteTimeButtons;
}

@end
